package com.tys.algorithm.beginner;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;

public class testStackLinkeList {

    public static void main(String[] args) {
        testLinkedListAndArrayList();

        testStackAndLinkedList();

        testStackAndArray();

//        //数组替代栈
//        int[] sta = new int[100];
//        int index = 0;
//        sta[index++] = 1;
//        sta[index++] = 2;
//        sta[index++] = 3;
//        System.out.println(sta[--index]);
//        System.out.println(sta[--index]);
//        System.out.println(sta[--index]);

    }

    //LinkedList和ArrayList对比
    public static void testLinkedListAndArrayList() {
        int testTime = 100000;
        long start;
        long end;

        System.out.println("hello");

        //加入头的时候LinkedList比ArrayList要快很多
        ArrayList<Integer> arr1 = new ArrayList<>();
        start = System.currentTimeMillis();
        for (int i = 0; i < testTime; i++) {
            arr1.add(0, i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);

        LinkedList<Integer> arr2 = new LinkedList<>();
        start = System.currentTimeMillis();
        for (int i = 0; i < testTime; i++) {
            arr2.add(0, i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);
        System.out.println("====================");
    }

    //Stack和LinkedList对比
    public static void testStackAndLinkedList() {
        int testTime = 100000;
        long start;
        long end;

        System.out.println("hello");

        //Stack和LinkedList速度测试
        testTime = 100000;
        Stack<Integer> stack = new Stack<>();
        //Stack入栈
        start = System.currentTimeMillis();
        for (int i = 0; i < testTime; i++) {
            stack.add(0, i);
        }
        end = System.currentTimeMillis();
        System.out.println("Stack入栈时间：" + (end - start));
        //Stack出栈
        start = System.currentTimeMillis();
        while (!stack.isEmpty()) {
            stack.pop();
        }
        end = System.currentTimeMillis();
        System.out.println("Stack出栈时间：" + (end - start));

        //LinkedList替代栈
        LinkedList<Integer> s = new LinkedList<>();
        //LinkedList入栈
        start = System.currentTimeMillis();
        for (int i = 0; i < testTime; i++) {
            s.addLast(i);
        }
        end = System.currentTimeMillis();
        System.out.println("LinkedList入栈时间：" + (end - start));
        //LinkedList出栈
        start = System.currentTimeMillis();
        while (!s.isEmpty()) {
            s.pollLast();
        }
        end = System.currentTimeMillis();
        System.out.println("LinkedList出栈时间：" + (end - start));
    }

    //Stack和数组对比
    public static void testStackAndArray() {
        int testTime = 100000;
        long start;
        long end;

        System.out.println("hello");

        //Stack和LinkedList速度测试
        testTime = 100000;
        Stack<Integer> stack = new Stack<>();
        //Stack入栈
        start = System.currentTimeMillis();
        for (int i = 0; i < testTime; i++) {
            stack.add(0, i);
        }
        end = System.currentTimeMillis();
        System.out.println("Stack入栈时间：" + (end - start));
        //Stack出栈
        start = System.currentTimeMillis();
        while (!stack.isEmpty()) {
            stack.pop();
        }
        end = System.currentTimeMillis();
        System.out.println("Stack出栈时间：" + (end - start));

        //数组替代栈
        int[] s = new int[testTime];
        int index = 0;
        //数组入栈
        start = System.currentTimeMillis();
        for (int i = 0; i < testTime; i++) {
            s[index++] = i;
        }
        end = System.currentTimeMillis();
        System.out.println("数组入栈时间：" + (end - start));
        //数组出栈
        start = System.currentTimeMillis();
        while (index != 0) {
            int a = s[--index];
        }
        end = System.currentTimeMillis();
        System.out.println("数组出栈时间：" + (end - start));
    }
}
